var Rating={
	init: function( config ){
		this.config = config;
		console.log('initiated');
		console.log(config);
		this.updateRating(this.config.ratingval.attr('value'));
		this.bindEvents();
		
		
	},

	updateRating:function(val){
		var self = Rating;
		var posunutie = -300+(val/5)*300;
		self.config.ratingval.attr('value',val);
		self.displayRating();
		console.log(posunutie);
		self.config.ratingarea.css('background-position',posunutie+'px');

	},
	bindEvents: function(){
		console.log('binded')
		this.config.stars.on('click', this.rate);
	},
	appendError: function(){
		$('div.rating-error').html('<div class="alert alert-info"><h5><strong>INFO !!!</strong>  Už ste raz tento projekt hodnotili</h5></div>');

	},
	displayRating: function(){
		var self = Rating;
		var val = self.config.ratingval.attr('value');
		self.config.rating_val.html(Math.round(val*100)/100 + "/5");
	},


	

	rate: function(){
		
		var self = Rating;
		


		var value = $(this).data('val');
		self.config.ajaxform.find('input#ajaxRating').attr('value',value);

		$.ajax({
			url:"../rate_program",
			type:"POST",
			data: self.config.ajaxform.serialize(),
			success: function( results ){
				if(results == -1){
					self.appendError();

				}else{
					self.updateRating(results);	
				}
				
				
			
			},
		

			});

	},

};

Rating.init({
	ratingarea : $('div.rating'),
	stars :$('div.star'),
	ratingval: $('input#ratingval'),
	ajaxform : $('form#ratingAjax'),
	rating_val : $('span.rating-val'),

	
	}
	);